﻿#include "datatransfer.h"
#include "mainView.h"
#include "utility.h"
#include <thread>

int main(int argc, char *argv[])
{    

    //// 确保控制台窗口可见
    //AllocConsole();
    //FILE* dummy;
    //freopen_s(&dummy, "CONOUT$", "w", stdout);
    //freopen_s(&dummy, "CONOUT$", "w", stderr);

    // 从文本文件读取服务器IP和端口号
    std::string serverIP;
    int serverPort;
    readConfig(serverIP, serverPort);
    droneData.serverIP = serverIP;
    droneData.serverPort = serverPort;

    //std::thread udpThread(udpServer,serverIP, serverPort);

    bool viewRuning = true;
    initMainView();

    for (; viewRuning == true && is_run(); delay_fps(90)) {
        cleardevice();
        // 切换模式
        if (keystate(key_1) && droneData.modeControl == false) {
            droneData.modeControl = true;
        }
        if (keystate(key_2) && droneData.modeControl == true) {
            droneData.modeControl = false;
        }

        if (droneData.modeControl)
        {
            keyboardControl();
            JoystickControl();
        }
            
        udpServer(serverIP, serverPort);
    	drawMainView();

        // 检测ESC键退出
        if (keystate(key_esc)) {
            break;
        }

        //// 模拟数据时输出
        //if (droneData.isConnected)
        //    printDashboard(droneData);
    }
    settextjustify(CENTER_TEXT, CENTER_TEXT);
    setcolor(RED);
    outtextxy(params.width / 2.0, params.height / 2.0, "正在退出中......");
    Sleep(1000);  // 给用户时间查看退出消息

    closeMainView();
    closegraph();
    //udpThread.join();
    return 0;
}